package com.quectel.mobile.controller;

import com.quectel.constant.global.UploadConstants;
import com.quectel.util.common.ImageUtils;
import com.quectel.util.common.MyFileUtils;
import com.quectel.util.common.Response;
import com.quectel.util.kit.StorageKit;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("upload")
public class UploadController {

    @PostMapping("upImgWithCompress")
    @Operation(summary = "图片上传 后端压缩 ")
    public Response<Object> upImgWithCompress(MultipartHttpServletRequest request) {

        Iterator<String> fileNames = request.getFileNames();
        while (fileNames.hasNext()) {
            String next = fileNames.next();
            List<MultipartFile> files = request.getFiles(next);
            for (MultipartFile file : files) {
                int i = 0;
                for (i = 0; i < UploadConstants.FILE_ACCEPT_TYPE.length; i++) {
                    if (file.getOriginalFilename().endsWith(UploadConstants.FILE_ACCEPT_TYPE[i])) {
                        break;
                    }
                }
                if (i >= UploadConstants.FILE_ACCEPT_TYPE.length) {
                    return Response.error("不支持的文件格式");
                }
                File tempFile = null;
                try {
                    tempFile = ImageUtils.compressImgWriteToTempDir(file.getInputStream(), file.getOriginalFilename(), false);
                    String url = StorageKit.uploadToPublicBucketObtainUrl(tempFile);
                    return Response.ok().wrap(url);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (tempFile != null && tempFile.exists()) {
                        tempFile.delete();
                    }
                }
            }
        }
        return Response.error("上传失败");
    }

    @PostMapping("upImg")
    @Operation(summary = "图片上传不压缩")
    public Response<Object> upImg(MultipartHttpServletRequest request) {

        Iterator<String> fileNames = request.getFileNames();
        while (fileNames.hasNext()) {
            String next = fileNames.next();
            List<MultipartFile> files = request.getFiles(next);
            for (MultipartFile file : files) {
                int i = 0;
                for (i = 0; i < UploadConstants.FILE_ACCEPT_TYPE.length; i++) {
                    if (file.getOriginalFilename().endsWith(UploadConstants.FILE_ACCEPT_TYPE[i])) {
                        break;
                    }
                }
                if (i >= UploadConstants.FILE_ACCEPT_TYPE.length) {
                    return Response.error("不支持的文件格式");
                }
                File tempFile = null;
                try {
                    tempFile = ImageUtils.compressImgWriteToTempDir(file.getInputStream(), file.getOriginalFilename(), false);
                    String url = StorageKit.uploadToPublicBucketObtainUrl(tempFile);
                    return Response.ok().wrap(url);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (tempFile != null && tempFile.exists()) {
                        tempFile.delete();
                    }
                }
            }
        }
        return Response.error("上传失败");
    }

    @PostMapping("upFile")
    @Operation(summary = "文件上传 ")
    public Response<Object> upFile(MultipartHttpServletRequest request) {

        Iterator<String> fileNames = request.getFileNames();
        while (fileNames.hasNext()) {
            String next = fileNames.next();
            List<MultipartFile> files = request.getFiles(next);
            for (MultipartFile file : files) {
                int i = 0;
                for (i = 0; i < UploadConstants.FILE_ACCEPT_TYPE.length; i++) {
                    if (file.getOriginalFilename().endsWith(UploadConstants.FILE_ACCEPT_TYPE[i])) {
                        break;
                    }
                }
                if (i >= UploadConstants.FILE_ACCEPT_TYPE.length) {
                    return Response.error("不支持的文件格式");
                }
                File tempFile = null;
                try {
                    tempFile = MyFileUtils.writeFileToTempDir(file.getInputStream(), file.getOriginalFilename(), false);
                    String url = StorageKit.uploadToPublicBucketObtainUrl(tempFile);
                    return Response.ok().wrap(url);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (tempFile != null && tempFile.exists()) {
                        tempFile.delete();
                    }
                }
            }
        }
        return Response.error("上传失败");
    }

    @PostMapping("upBase64Image")
    @Operation(summary = "头像上传 ")
    public Response<Object> upBase64Image(@RequestBody String base64Data) {
        File file = ImageUtils.base64ImgToTempFile(base64Data);
        String url = StorageKit.uploadToPublicBucketObtainUrl(file);
        file.delete();
        return Response.ok().wrap(url);
    }
}
